Page({
  data: {
    orderList: [],
    pendingOrders: [],
    paidOrders: [],
    completedOrders: [],
    currentStatus: 'all', // 当前显示的状态：all, pending, paid, completed
    displayOrders: [] // 当前显示的订单列表
  },
  onLoad(options) {
    // 获取传入的状态参数
    if (options.status) {
      this.setData({ currentStatus: options.status });
    }
  },
  onShow() {
    this.getOrderList();
  },
  getOrderList() {
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({ title: '请先登录', icon: 'none' });
      return;
    }
    
    wx.request({
      url: 'http://localhost:3000/api/order',
      method: 'GET',
      data: { user_id: userInfo.id },
      success: (res) => {
        console.log('订单数据', res.data);
        if (res.data && res.data.code === 0) {
          const orderList = res.data.data;
          const pendingOrders = orderList.filter(o => o.status === '待支付');
          const paidOrders = orderList.filter(o => o.status === '已支付');
          const completedOrders = orderList.filter(o => o.status === '已完成');
          
          this.setData({ 
            orderList, 
            pendingOrders, 
            paidOrders, 
            completedOrders 
          });
          
          // 更新显示列表
          this.updateDisplayOrders();
        }
      }
    });
  },
  setCurrentStatus(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({ currentStatus: status });
    this.updateDisplayOrders();
  },
  // 根据当前状态更新显示列表
  updateDisplayOrders() {
    const { currentStatus, pendingOrders, paidOrders, completedOrders } = this.data;
    let displayOrders = [];
    
    switch (currentStatus) {
      case 'pending':
        displayOrders = pendingOrders;
        break;
      case 'paid':
        displayOrders = paidOrders;
        break;
      case 'completed':
        displayOrders = completedOrders;
        break;
      default:
        displayOrders = [...pendingOrders, ...paidOrders, ...completedOrders];
    }
    
    this.setData({ displayOrders });
  },
  payOrder(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.request({
      url: 'http://localhost:3000/api/order/pay',
      method: 'POST',
      data: { order_id: orderId },
      success: (res) => {
        if (res.data && res.data.code === 0) {
          wx.showToast({ title: '支付成功', icon: 'success' });
          this.getOrderList();
        }
      }
    });
  },
  deleteOrder(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.request({
      url: 'http://localhost:3000/api/order/' + orderId,
      method: 'DELETE',
      success: (res) => {
        if (res.data && res.data.code === 0) {
          wx.showToast({ title: '删除成功', icon: 'success' });
          this.getOrderList();
        }
      }
    });
  },
  viewDetail(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/user/order/detail?id=' + orderId
    });
  }
});